#include <iostream>
#include <string.h>
#include <queue>

using namespace std;

typedef enum {Link, Thread} NodeTag;

class TreeNode {
  private:
    char data;              //值
    TreeNode* leftChild;    //左孩子
    TreeNode* rightChild;   //右孩子
    NodeTag lTag, rTag;     //线索Tag
    friend class MyTree;    //树类
  public:
    TreeNode(char, TreeNode*, TreeNode*, NodeTag, NodeTag);
    ~TreeNode();
    TreeNode(const TreeNode*);
    void printNode();

};

class MyTree {
  private:
    TreeNode* root;        //根指针
    bool isThread;         //线索二叉树标志
  public:
    MyTree();               //无参构造
    MyTree(const char[]);   //构造函数
    MyTree(const MyTree&);  //拷贝函数
    MyTree(const TreeNode*);
    ~MyTree();              //析构函数
    void deleteTree(TreeNode*);

    TreeNode* create_tree(const char*&);
    void preOrderTraverse();    //前序遍历
    void preOrder(TreeNode*);
    void inOrderTraverse();     //中序遍历
    void inOrder(TreeNode*);
    void inOrder_t(TreeNode*);
    void postOrderTraverse();   //后序遍历
    void postOrder(TreeNode*);
    int countLeaf();            //叶结点计数
    int count_leaf(TreeNode*);
    int countHeight();          //计算树的深度
    int count_height(TreeNode*);
    bool isThreadedTree();      //判断是否为线索二叉树
    bool inOrderThreading();    //线索化
    void Threading(TreeNode*,TreeNode*&);
    TreeNode& locateNode(const char&);  //结点定位
    TreeNode& preNode(const TreeNode&); //获取前驱结点
    TreeNode& nextNode(const TreeNode&);//获取后继结点
};

class HTNode {
  private:
    int weight;
    HTNode* parent;
    HTNode* leftchild;
    HTNode* rightchild;
    HTNode* next;
    string str;
    friend class HuffmanTree;
  public:
    HTNode(const int& num = 0);
    ~HTNode();
} ;

class HuffmanTree {
  private:
    HTNode* root;
  public:
    HuffmanTree(const int&,const int[]);    //构造函数
    ~HuffmanTree();     //析构函数
    void list_push(HTNode*);
    //输出哈夫曼编码
    //格式为：每行输出一个节点，节点排序递减
    //节点1值：编码
    //节点2值：编码
    //....
    //cpp中未给出该函数，遵守输出格式规范
    void printHuffmanCodes();

};


